package gangnam.control;

import gangnam.model.*;
import gangnam.utils.HibernateUtil;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class ActorsServlet
 */
public class ActorsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ActorsServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		try {
		 	//Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            
		 	// Begin unit of work
		 	
		 	//session.beginTransaction();

		 	HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
            // Process request and render page...
		 	Actors theActor = new Actors();
		 	theActor.setId(2);
		 	theActor.setFname("Coba Coba lah");
		 	theActor.setLname("Intranet lah");
		 	

            // End unit of work
		 	

	        //session.save(theActor);
	        HibernateUtil.getSessionFactory().getCurrentSession().save(theActor);

	        //session.getTransaction().commit();
	        if (! HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().wasCommitted())
	        	HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
        }
        catch (Exception ex) {
            HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
            if ( ServletException.class.isInstance( ex ) ) {
                throw ( ServletException ) ex;
            }
            else {
                throw new ServletException( ex );
            }
        }

		

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
	}

}
